<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
	<jsp:include page="/include.jsp"/>
	<script type="text/javascript">
	 $(document).ready(function(){
		 $('#framework_systemManager_department_treegrid').treegrid({
				url : '${pageContext.request.contextPath}/systemManagerAction!getEtlDepartmentData.action',
				idField : 'id',
				treeField : 'text',
				parentField : 'pid',
				fit : true,
				fitColumns : true,
				border : false,
				frozenColumns : [ [ {
					field : 'id',
					title : '部门编号', 
					width : 10,
					hidden : true
				}, {
					field : 'text',
					title : '部门名称',
					width : 200
				}] ],
				columns : [ [ {
					field : 'deptEnName',
					title : '部门简称',
					align:'center',
					width : 100
				}, {
					field : 'deptDesc',
					title : '部门描述',
					align:'center',
					width : 200
				}, {
					field : 'pdeptId',
					title : '上级部门编码',
					align:'center',
					hidden:true,
					width : 80
				},{
					field : 'pdeptName',
					title : '上级部门名称',
					align:'center',
					width : 80
				},{
					field : 'action',
					title : '操作',
					width : 80,
					align : 'center',
					formatter : function(value, row, index) {
						if(row.parentmoudle!="parent"){
							return  '<span  style="cursor:pointer" onclick="framework_department_edit(\''+row.id+'\')"><img  src="${pageContext.request.contextPath}/images/icons/modify_or_edit.png" title="修改" alt="修改" style="margin-top:2px;"/>修改&nbsp;&nbsp;</span>'		
			                       +'<span  style="cursor:pointer" onclick="framework_department_delete(\''+row.id+'\')"><img  src="${pageContext.request.contextPath}/images/icons/delete.png" title="删除" alt="删除" style="margin-top:2px;"/>删除&nbsp;&nbsp;</span>';
						}else{
							return '';
						}
					}
				} ] ],
				toolbar : [ {
					text : '新建',
					iconCls : 'icon-add',
					handler : function() {
						framework_department_add();
					}
				}, '-', {
					text : '展开',
					iconCls : 'icon-redo',
					handler : function() {
						var node = $('#framework_systemManager_department_treegrid').treegrid('getSelected');
						if (node) {
							$('#framework_systemManager_department_treegrid').treegrid('expandAll', node.cid);
						} else {
							$('#framework_systemManager_department_treegrid').treegrid('expandAll');
						}
					}
				}, '-', {
					text : '折叠',
					iconCls : 'icon-undo',
					handler : function() {
						var node = $('#framework_systemManager_department_treegrid').treegrid('getSelected');
						if (node) {
							$('#framework_systemManager_department_treegrid').treegrid('collapseAll', node.cid);
						} else {
							$('#framework_systemManager_department_treegrid').treegrid('collapseAll');
						}
					}
				}, '-', {
					text : '刷新',
					iconCls : 'icon-reload',
					handler : function() {
						$('#framework_systemManager_department_treegrid').treegrid('reload');
					}
				} ]
			});
	
	 });
	 //修改系统部门
	 function framework_department_edit(id) {
			if (id != undefined) {
				$('#framework_systemManager_department_treegrid').treegrid('select', id);
			}
			var node = $('#framework_systemManager_department_treegrid').treegrid('getSelected');
			$('<div/>').dialog({
				href : '${pageContext.request.contextPath}/systemManagerAction!modifyDepartment.action?deptId='+node.id,
				width : 550,
				top:($(window.top).height()-500)*0.5,
			    left:($(window.top).width()-800)*0.5,
				modal : true,
				title : '修改部门',
				buttons : [ {
					text : '保存',
					iconCls : '',
					handler : function() {
						var d = $(this).closest('.window-body');
						$('#framework_department_modify_editMenu').form('submit', {
							url : '${pageContext.request.contextPath}/systemManagerAction!modifyDepartmentData.action',
							onSubmit:function(){
								if ($(this).form("validate"))
								{
									window.top.$.messager.progress({title : '提示',text : '正在保存，请稍后 ....'});
								}
								else
								{
									return false;
								}
							},
							success : function(result) {
								try {
									var r = $.parseJSON(result);
									if (r.returnFlag) {
										$('#framework_systemManager_department_treegrid').treegrid('reload');
										d.dialog('destroy'); 
									}
									$.messager.show({
										title : '提示',
										msg : r.returnMsg
									});
									window.top.$.messager.progress('close'); 
								} catch (e) {
									$.messager.alert('提示', result);
									window.top.$.messager.progress('close'); 
								}
							}
						});
					}
				} ],
				onClose : function() {
					$(this).dialog('destroy');
					$('#framework_systemManager_department_treegrid').treegrid('reload');
				}
			});
		}
	 //增加部门
	 function framework_department_add() {
			$('<div/>').dialog({
				href : '${pageContext.request.contextPath}/framework/systemManager/department/department_manager_add.jsp',
				width : 600,
				top:($(window.top).height()-500)*0.5,
			    left:($(window.top).width()-800)*0.5,
				modal : true,
				title : '新增部门',
				buttons : [ {
					text : '保存',
					iconCls : '',
					handler : function() {
						var d = $(this).closest('.window-body');
						if ($("#framework_department_addMenu").form("validate"))
						{
							var deptName=$("#framework_department_deptName").val();
							$.ajax({
								 url : '${pageContext.request.contextPath}/systemManagerAction!validDeptName.action',
								 data:{"deptName":deptName},
								 dataType :'json',
								 cache : true,
								 beforeSend:function(){
									 window.top.$.messager.progress({title : '提示',text : '正在保存，请稍候 ....'});
								 },
								 success : function(r){ 
									 if (r.returnFlag) {
										 var str="*部门名称已存在*";
										 $("#framework_department_validdeptName").html(str);
										 window.top.$.messager.progress('close'); 
									 }else{
										 $("#framework_department_validdeptName").html("");
										 $('#framework_department_addMenu').form('submit', {
												url : '${pageContext.request.contextPath}/systemManagerAction!addDepartment.action',
												success : function(result) {
													try {
														var r = $.parseJSON(result);
														if (r.returnFlag) {
															d.dialog('destroy'); 
															$('#framework_systemManager_department_treegrid').treegrid('reload');
														}
														$.messager.show({
															title : '提示',
															msg : r.returnMsg
														});
														window.top.$.messager.progress('close'); 
													} catch (e) {
														$.messager.alert('提示', result);
														window.top.$.messager.progress('close'); 
													}
												}
											});
									 }
								 }
							 });
						}
					}
				} ],
				onClose : function() {
					$(this).dialog('destroy');
					$('#framework_systemManager_department_treegrid').treegrid('reload');
				}
			});
		}
	 //删除部门
	 function framework_department_delete(id) {
			if (id != undefined) {
				$('#framework_systemManager_department_treegrid').treegrid('select', id);
			}
			var node = $('#framework_systemManager_department_treegrid').treegrid('getSelected');
			
			 $.ajax({
				 url : '${pageContext.request.contextPath}/systemManagerAction!ValidDepartMentContainUser.action',
				 data:{deptId : node.id},
				 dataType :'json',
				 cache : true,
				 success : function(r){ 
					 if (r.returnFlag) {
						 $.messager.show({
								title : '提示',
								msg : r.returnMsg
						 });
					 }else{
						 if (node) {
							 $.messager.confirm('询问', '您确定要删除【' + node.text + '】？', function(b) {
									if (b) {
										$.ajax({
											url : '${pageContext.request.contextPath}/systemManagerAction!deleteDepartment.action',
											data : {deptId : node.id},
											cache : false,
											dataType : 'JSON',
											success : function(r) {
												if (r.returnFlag) {
													$('#framework_systemManager_department_treegrid').treegrid('reload');
												}
												$.messager.show({
													title : '提示',
													msg : r.returnMsg
												});
												
											}
										});
									}
								});	
						 }
					 }
					
				 }
		     });
	  }
	</script>
</head>

<body >
   <table id="framework_systemManager_department_treegrid"></table> 
</body>
</html>
